using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Processors;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Compiler;

namespace ModelProcessor
{
    [ContentProcessor(DisplayName = "Innovation Engine Model Processor")]
    public class IEModelProcessor : Microsoft.Xna.Framework.Content.Pipeline.Processors.ModelProcessor
    {
        public override ModelContent Process(NodeContent input, ContentProcessorContext context)
        {
            // Create the content like normal
            ModelContent content = base.Process(input, context);

            // For each mesh...
            foreach (ModelMeshContent mesh in content.Meshes)
            {
                // Create a variable to keep track of MeshParts
                int i = 0;

                // For each MeshPart...
                foreach (ModelMeshPartContent meshPart in mesh.MeshParts)
                {
                    // Create a string array that will hold data about the MeshPart
                    string[] id = new string[2];

                    // The first part of the array is the name of the MeshPart
                    id[0] = mesh.Name + ".MeshPart" + i;

                    // For each texture...
                    foreach (KeyValuePair<string, ExternalReference<TextureContent>> texture in meshPart.Material.Textures)
                    {
                        // Get the content filename
                        string textureName = "Content\\" + texture.Value.Filename.Replace(context.OutputDirectory, "").Replace(".xnb", "");

                        // Set the second part of the data array to the texture name
                        id[1] = textureName;
                    }

                    // Set the data array as the MeshPart's tag
                    meshPart.Tag = id;

                    // Increment the MeshPart counter
                    i++;
                }
            }

            return content;
        }
    }
}